layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;

    /**
     * 养殖池管理
     */
    var Pood = {
        tableId: "poodTable"
    };

    //加载养殖场
    var farmDict = {};
    loadFarmList();
    function loadFarmList() {

        $("#farmId").html('<option value="">全部养殖场</option>');
        var ajax = new $ax(Feng.ctxPath + "/api/farm/list", function (data) {
            for (var i = 0; i < data.data.length; i++) {
                var name = data.data[i].title;
                var value = data.data[i].farmId;
                $("#farmId").append('<option value="' + value + '" >' + name + '</option>');
                farmDict[value] = name;
            }
            form.render();
        });

        ajax.start();
    }

    /**
     * 初始化表格的列
     */
    Pood.initColumn = function () {
        return [[
            {field: 'poodId', hide: true, title: 'ID'},
            {field: 'farmId', sort: true, title: '养殖场', templet:function (d) {
                    var desc = farmDict[d.farmId.toString()];
                    return  '<span class="layui-badge layui-bg-green">'+desc+'</span>';
                }},
            {field: 'title', sort: true, title: '养殖池标题'},
            {field: 'ph', sort: true, title: 'ph值范围'},
            {field: 'temperature', sort: true, title: '温度范围'},
            {field: 'salt', sort: true, title: '盐度范围'},
            {field: 'nitrate', sort: true, title: '亚硝酸盐'},
            {field: 'ammonia', sort: true, title: '氨氮'},
            {field: 'updateTime', sort: true, title: '更新时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };



    /**
     * 点击查询按钮
     */
    Pood.search = function () {
        var queryData = {};
        queryData['farmId'] = $("#farmId").val();
        queryData['title'] = $("#title").val();
        table.reload(Pood.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Pood.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/api/pood/edit';
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Pood.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/api/pood/edit?poodId=' + data.poodId
    };

    /**
     * 导出excel按钮
     */
    Pood.exportExcel = function () {
        var checkRows = table.checkStatus(Pood.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Pood.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/api/pood/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Pood.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("poodId", data.poodId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Pood.tableId,
        url: Feng.ctxPath + '/api/pood/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        method: "post",
        cols: Pood.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Pood.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Pood.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Pood.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Pood.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Pood.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Pood.onDeleteItem(data);
        }
    });
});
